package meterMonitor.biz;

import meterMonitor.mapper.*;
import meterMonitor.model.*;
import meterMonitor.util.BizUtil;
import meterMonitor.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2019/1/16 0016.
 */
@Service
public class IntegrateBiz extends BaseBiz {

    @Autowired
    MeterMapper meterMapper;
    @Autowired
    MonitorMapper monitorMapper;
    @Autowired
    MonitorHourMapper monitorHourMapper;
    @Autowired
    BizUtil bizUtil;
    @Autowired
    private MonitorDayMapper monitorDayMapper;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private MonitorMonthMapper monitorMonthMapper;


    public TodayWater queryTodayElectricity() {
        TodayWater todayWater = new TodayWater();
        /**
         * SimpleDateFormat 是一个格式化Date 以及 解析日期字符串 的工具。
         *
         * 它的最常用途是，能够按照指定的格式来对Date进行格式化，然后我们使用可以格式化Date后得到的字符串。
         * 更严格的说，SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类。
         *
         * 它允许进行格式化（日期 -> 文本）、解析（文本 -> 日期）和规范化。
         */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 查询当前时间
        Calendar instance = Calendar.getInstance();
        Date current = instance.getTime();
        String day = sdf.format(current);



        // 设置总的水表数量
        List<Meter> meters = meterMapper.queryAllMeterElectricity();
        //总个数等于查询出的总仪器数
        todayWater.setTotalCnt(meters.size());
        //求所有仪器的日标准量的和
        double totalStandard = meters.stream().mapToDouble(Meter::getStandard).sum();
        //实际用水量
        double totalUsage = 0d;
        int overUsage = 0;
        //查询当前时间（日）监控的数据
        List<MonitorDay> monitorDays = monitorDayMapper.queryElectricityByDay(day);
        if (null != monitorDays && monitorDays.size() > 0) {
            for (MonitorDay md : monitorDays) {
                //求间隔使用量的总和
                totalUsage += md.getIntervalQuantity();
                //如果取得的日间隔使用量大于标准量
                if (md.getIntervalQuantity() > md.getStandard()) {
                    overUsage++;//统计标签：统计超出标准量的总数
                }
            }
        }

        // 设置当月用水总量
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        MonitorMonth monitorMonth = new MonitorMonth();
        monitorMonth.setMonth(sdf2.format(current));
        //查询当月的监控数据（meter_code：读表器编号
        //    interval_quantity：间隔使用量
        //    month：日期（按月）
        //    device_name：设备名称
        //    another_name：客户编号
        //    area_id:设备所属区域id
        //    area_name:区名称
        //    standard:日标准量）
        List<MonitorMonth> monthList = monitorMonthMapper.queryElectricity(monitorMonth);
        //获取当月的最后一天
        int actualMaximum = instance.getActualMaximum(Calendar.DAY_OF_MONTH);

        for (MonitorMonth m : monthList) {
            //设置当月的月标准量
            m.setStandard(m.getStandard() * actualMaximum);
        }
        //当月用水总量--按设备位置统计
        todayWater.setMonitorMeters(monthList);
        //获取所有的区名称
        List<Area> allAreas = areaMapper.getAllAreas();
        //DecimalFormat 用于将一个数字进行格式化成字符串；#：没有则为空；0:没有则用0代替
        DecimalFormat df = new DecimalFormat("#0.0");
        // 设置当前标准用水量;Double.valueOf：字符串转换为浮点数
        todayWater.setStandardWater(Double.valueOf(df.format(totalStandard)));
        // 设置当前实际用水量
        todayWater.setCurrentWater(Double.valueOf(df.format(totalUsage)));
        // 统计超标数量
        todayWater.setOverCnt(overUsage);
        // 设置区域
        todayWater.setAreas(allAreas);
        // 设置实际
        todayWater.setDataStr("今日");
        //获取当天是这个月的第几天
        int currentDaySeq = instance.get(Calendar.DAY_OF_MONTH);
        //小于十天按十天算
        if (currentDaySeq <= 10) {
            currentDaySeq = 10;
        }
        //当前天替换一天
        instance.set(Calendar.DAY_OF_MONTH, 1);
        //查询当前时间：年-月-日
        String monthStart = sdf.format(instance.getTime());
        //用当月最后一天日期的值取代当前日
        instance.set(Calendar.DAY_OF_MONTH, instance.getActualMaximum(Calendar.DAY_OF_MONTH));
        //查询当前时间：年-月-日
        String monthEnd = sdf.format(instance.getTime());

        List<MonitorDay> list = new ArrayList<>(); // monitorDayMapper.countByDay(monthStart, monthEnd);
        //求在线仪器的日标准量的和
        double meterStandards = meterMapper.queryAllMeterElectricity().stream().mapToDouble(Meter::getStandard).sum();
        //循环遍历当前日期在当月的天数
        for (int i = 1; i <= currentDaySeq; i++) {
            //用i的值取代当前日
            instance.set(Calendar.DAY_OF_MONTH, i);
            //当前时间
            String param = sdf.format(instance.getTime());
            //查询当前时间的间隔使用量
            MonitorDay monitorDay = monitorDayMapper.countElectricityByOneDay(param);
            //如果monitorDay为空
            if (Objects.isNull(monitorDay)) {
                monitorDay = new MonitorDay();
                monitorDay.setIntervalQuantity(0D);
                monitorDay.setDay(param);
            }
            //不为空时，将在线仪器的日标准量的和设置到monitorDay中
            monitorDay.setStandard(meterStandards);
            //添加到集合中
            list.add(monitorDay);
        }
        //求list中MonitorDay的间隔使用量的总和
        double monthTotal = list.stream().mapToDouble(MonitorDay::getIntervalQuantity).sum();
        //设置实时流量-立方米/h
        todayWater.setAvg1(monthTotal);
        //设置当月用水总量--按天统计
        todayWater.setMonitorCurrentMonths(list);
        return todayWater;
    }

    /**
     * 查询当日用电量
     * @return
     */
    public TodayElectricity queryTodayElectricity1(){
        TodayElectricity todayElectricity=new TodayElectricity();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 查询当前时间
        Calendar instance = Calendar.getInstance();
        Date current = instance.getTime();
        String day = sdf.format(current);
        // 设置总的在线电表数量
        List<Meter> meters = meterMapper.queryAllMeterElectricity();
        todayElectricity.setTotalCnt1(meters.size());
        //求总的日标准量
        double totalStandard=meters.stream().mapToDouble(Meter::getStandard).sum();
        //设置实际用电量
        double totalUsage = 0d;
        int overUsage = 0;

        //查询当前时间（日）监控的数据
        List<MonitorDay> monitorDays = monitorDayMapper.queryElectricityByDay(day);
        if (null != monitorDays && monitorDays.size() > 0) {
            for (MonitorDay md : monitorDays) {
                //求间隔使用量的总和
                totalUsage += md.getIntervalQuantity();
                //如果取得的日间隔使用量大于标准量
                if (md.getIntervalQuantity() > md.getStandard()) {
                    overUsage++;//统计标签：统计超出标准量的总数
                }
            }
        }
        // 设置当月用电总量
        //取时间
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        MonitorMonth monitorMonth = new MonitorMonth();
        monitorMonth.setMonth(sdf2.format(current));
        //查询当月的监控数据（meter_code：读表器编号
        //    interval_quantity：间隔使用量
        //    month：日期（按月）
        //    device_name：设备名称
        //    another_name：客户编号
        //    area_id:设备所属区域id
        //    area_name:区名称
        //    standard:日标准量）
        List<MonitorMonth> monthList = monitorMonthMapper.queryElectricity(monitorMonth);
        //获取当月的最后一天
        int actualMaximum = instance.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (MonitorMonth m : monthList) {
            //设置当月的月标准量
            m.setStandard(m.getStandard() * actualMaximum);
        }
        //当月用电总量--按设备位置统计
        todayElectricity.setMonitorMeters(monthList);
        //获取所有的区名称
        List<Area> allAreas = areaMapper.getAllAreas();
        //DecimalFormat 用于将一个数字进行格式化成字符串；#：没有则为空；0:没有则用0代替
        DecimalFormat df = new DecimalFormat("#0.0");
        // 设置当前标准用电量;Double.valueOf：字符串转换为浮点数
        todayElectricity.setStandardElectricity(Double.valueOf(df.format(totalStandard)));
        // 设置当前实际用水量
        todayElectricity.setCurrentElectricity(Double.valueOf(df.format(totalUsage)));
        // 统计超标数量
        todayElectricity.setOverCnt1(overUsage);
        // 设置区域
        todayElectricity.setAreas(allAreas);
        // 设置实际
        todayElectricity.setDataStr("今日");
        //获取当天是这个月的第几天
        int currentDaySeq = instance.get(Calendar.DAY_OF_MONTH);
        //小于十天按十天算
        if (currentDaySeq <= 10) {
            currentDaySeq = 10;
        }
        //当前天替换一天
        instance.set(Calendar.DAY_OF_MONTH, 1);
        //查询当前时间：年-月-日
        String monthStart = sdf.format(instance.getTime());
        //用当月最后一天日期的值取代当前日
        instance.set(Calendar.DAY_OF_MONTH, instance.getActualMaximum(Calendar.DAY_OF_MONTH));
        //查询当前时间：年-月-日
        String monthEnd = sdf.format(instance.getTime());

        List<MonitorDay> list = new ArrayList<>(); // monitorDayMapper.countByDay(monthStart, monthEnd);
        //求在线电表仪器的日标准量的和
        double meterStandards = meterMapper.queryAllMeterElectricity().stream().mapToDouble(Meter::getStandard).sum();
        //循环遍历当前日期在当月的天数
        for (int i = 1; i <= currentDaySeq; i++) {
            //用i的值取代当前日
            instance.set(Calendar.DAY_OF_MONTH, i);
            //当前时间
            String param = sdf.format(instance.getTime());
            //查询当前时间的间隔使用量
            MonitorDay monitorDay = monitorDayMapper.countElectricityByOneDay(param);
            //如果monitorDay为空
            if (Objects.isNull(monitorDay)) {
                monitorDay = new MonitorDay();
                monitorDay.setIntervalQuantity(0D);
                monitorDay.setDay(param);
            }
            //不为空时，将在线仪器的日标准量的和设置到monitorDay中
            monitorDay.setStandard(meterStandards);
            //添加到集合中
            list.add(monitorDay);
        }
        //求list中MonitorDay的间隔使用量的总和
        double monthTotal = list.stream().mapToDouble(MonitorDay::getIntervalQuantity).sum();
        //设置实时流量-立方米/h
        todayElectricity.setAvg(monthTotal);
        //设置当月用电总量--按天统计
        todayElectricity.setMonitorCurrentMonths(list);

        return todayElectricity;
    }
    /**
     * 电表的用电节约率
     * @return
     * @throws ParseException
     */
    public List<MonitorMeter> queryMonthElectricity() throws ParseException{
        //DecimalFormat 用于将一个数字进行格式化成字符串；#：没有则为空；0:没有则用0代替
        DecimalFormat df = new DecimalFormat("#0.0");
        //指定日期的格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //查询当前时间
        Calendar instance = Calendar.getInstance();

        instance.set(Calendar.MONTH, 0);//控制月
        instance.set(Calendar.DAY_OF_MONTH, 1);//控制天数
        instance.set(Calendar.HOUR_OF_DAY, 0);//控制时
        instance.set(Calendar.MINUTE, 0);//控制分
        instance.set(Calendar.SECOND, 0);//控制秒
        MonitorMonth param = new MonitorMonth();
        //getTimeInMillis()方法用于获取此Calendar的毫秒数。设置param的上传时间
        param.setUploadTime(new Timestamp(instance.getTimeInMillis()));
        //查出月份电表的总间隔使用量
        List<MonitorMonth> monitorMonths = monitorMonthMapper.countElectricityByMonth(param);
        //新建一个对象集合：一个电表本月用电量
        List<MonitorMeter> monitorMeters = new ArrayList<>();
        //统计电表日标准总值
        Double standard = meterMapper.sumStandardElectricity();
        //查询当前时间
        Calendar cc = Calendar.getInstance();
        //遍历月份的总间隔使用量
        for (MonitorMonth m : monitorMonths) {
            //新建一个对象：一个电表本月用电量
            MonitorMeter mm = new MonitorMeter();
            mm.setUploadDateStr(m.getMonth());//设置上传日期
            mm.setIntervalQuantity(m.getIntervalQuantity());//设置电表本月使用量

            // 获取当月总天数
            cc.setTime(sdf.parse(m.getMonth()));
            //当月天数
            int monthDays = cc.getActualMaximum(Calendar.DAY_OF_MONTH);
            // 算月标准值
            Double totalStandard = standard * monthDays;
            //设置当前月标准用电量
            mm.setMonthMeterStandar(Double.valueOf(df.format(totalStandard)));
            //设置日标准量
            mm.setStandard(totalStandard);
            //如果间隔使用量大于月标准量
            if (m.getIntervalQuantity() >= totalStandard) {
                //将节约率设为0
                mm.setEconomy(0);
                //设置成0%
                mm.setStrEconomy(mm.getEconomy() * 100 + "%");
            } else {
                //设置节约率
                mm.setEconomy(Double.valueOf(df.format((totalStandard - m.getIntervalQuantity()) / totalStandard)));
                mm.setStrEconomy(mm.getEconomy() * 100 + "%");
            }
            monitorMeters.add(mm);
        }
        //返回电表的本月用电量数据
        return monitorMeters;
    }
    /**
     * 查询当月电表超标数统计
     * @return
     */
    public MeterOverMonthVo queryCurrentMonthOverListElectricity(){

        MeterOverMonthVo overVo = new MeterOverMonthVo();
        //设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //转换成字符串格式的日期
        String currentMonth = sdf.format(new Date());
        //查询当月的用量信息
        List<MonitorMonth> monitorMonths = monitorMonthMapper.queryElectricityByMonth(currentMonth);
        //设置超标个数标签；
        int overCnt = 0;
        //查询当前时间
        Calendar instance = Calendar.getInstance();
        //声明封装超标电表情况的集合
        List<SuperMeter> overList = new ArrayList<>();
        //DecimalFormat 用于将一个数字进行格式化成字符串；#：没有则为空；0:没有则用0代替
        DecimalFormat df = new DecimalFormat("#0.0");
        for (MonitorMonth m : monitorMonths) {
            //当月天数
            int monthDays = instance.getActualMaximum(Calendar.DAY_OF_MONTH);

            int currentDaySeq = instance.get(Calendar.DAY_OF_MONTH);
            //当月标准用电量
            Double totalStandard = m.getStandard() * currentDaySeq;
            //如果实际大于标准
            if (m.getIntervalQuantity() > totalStandard) {
                overCnt++;//统计超标次数
                //超标量
                double superWater = m.getIntervalQuantity() - totalStandard;
                //超标百分比
                double rate = superWater * 100 / totalStandard;
                //字符串拼接成小数点两位的百分数
                String superRate = df.format(rate) + "%";

                SuperMeter sm = new SuperMeter();

                sm.setAnotherName(m.getAnotherName());
                sm.setDeviceName(m.getDeviceName());
                sm.setStandard(totalStandard);
                sm.setSuperRate(superRate);
                sm.setSuperWater(superWater);
                sm.setIntervalQuantity(m.getIntervalQuantity());


                sm.setAreaId(m.getAreaId());
                sm.setMeterCode(m.getMeterCode());
                overList.add(sm);
            }
        }
        overVo.setOverCnt(overCnt);
        overVo.setOverList(overList);
        overVo.setTotalCnt(monitorMonths.size());
        return overVo;
    }

    //查询当月超标数统计
    public MeterOverMonthVo queryCurrentMonthOverList() throws Exception {

        MeterOverMonthVo overVo = new MeterOverMonthVo();
        //设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //转换成字符串格式的日期
        String currentMonth = sdf.format(new Date());
        //查询当月的用量信息
        List<MonitorMonth> monitorMonths = monitorMonthMapper.queryByMonth(currentMonth);
        //设置超标个数标签；
        int overCnt = 0;
        //查询当前时间
        Calendar instance = Calendar.getInstance();
        //声明封装超标水表情况的集合
        List<SuperMeter> overList = new ArrayList<>();
        //DecimalFormat 用于将一个数字进行格式化成字符串；#：没有则为空；0:没有则用0代替
        DecimalFormat df = new DecimalFormat("#0.0");
        for (MonitorMonth m : monitorMonths) {
            //当月天数
            int monthDays = instance.getActualMaximum(Calendar.DAY_OF_MONTH);

            //今天是当月的第几天，即是当月已经过了几天
            int currentDaySeq = instance.get(Calendar.DAY_OF_MONTH);

            //单个仪器当月标准用水量
            Double totalStandard = m.getStandard() * currentDaySeq;
            //如果实际大于标准
            if (m.getIntervalQuantity() > totalStandard) {
                overCnt++;//统计超标次数
                //超标量
                double superWater =Double.valueOf(df.format(m.getIntervalQuantity()- totalStandard) );
                //超标百分比
                double rate = superWater * 100 / totalStandard;
                //字符串拼接成小数点两位的百分数
                String superRate = df.format(rate) + "%";

                SuperMeter sm = new SuperMeter();

                sm.setAnotherName(m.getAnotherName());
                sm.setDeviceName(m.getDeviceName());
                sm.setStandard(totalStandard);
                sm.setSuperRate(superRate);
                sm.setSuperWater(superWater);

                sm.setIntervalQuantity(m.getIntervalQuantity());

                sm.setAreaId(m.getAreaId());
                sm.setMeterCode(m.getMeterCode());
                overList.add(sm);
            }
        }
        overVo.setOverCnt(overCnt);
        overVo.setOverList(overList);
        overVo.setTotalCnt(monitorMonths.size());
        return overVo;
    }

    //解耦
    public MeterOverMonthVo queryCurrentMonth(int deviceTypeCode) throws Exception{
        MeterOverMonthVo overVo = new MeterOverMonthVo();
        //设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //转换成字符串格式的日期
        String currentMonth = sdf.format(new Date());
        //查询当月的用量信息
        List<MonitorMonth> monitorMonths = monitorMonthMapper.queryByMonth1(currentMonth,deviceTypeCode);
        //设置超标个数标签；
        int overCnt = 0;
        //查询当前时间
        Calendar instance = Calendar.getInstance();
        //声明封装超标水表情况的集合
        List<SuperMeter> overList = new ArrayList<>();
        //DecimalFormat 用于将一个数字进行格式化成字符串；#：没有则为空；0:没有则用0代替
        DecimalFormat df = new DecimalFormat("#0.0");
        for (MonitorMonth m : monitorMonths) {
            //当月天数
            int monthDays = instance.getActualMaximum(Calendar.DAY_OF_MONTH);

            //今天是当月的第几天，即是当月已经过了几天
            int currentDaySeq = instance.get(Calendar.DAY_OF_MONTH);

            //单个仪器当月标准用水量
            Double totalStandard = m.getStandard() * currentDaySeq;
            //如果实际大于标准
            if (m.getIntervalQuantity() > totalStandard) {
                overCnt++;//统计超标次数
                //超标量
                double superWater =Double.valueOf(df.format(m.getIntervalQuantity()- totalStandard) );
                //超标百分比
                double rate = superWater * 100 / totalStandard;
                //字符串拼接成小数点两位的百分数
                String superRate = df.format(rate) + "%";

                SuperMeter sm = new SuperMeter();

                sm.setAnotherName(m.getAnotherName());
                sm.setDeviceName(m.getDeviceName());
                sm.setStandard(totalStandard);
                sm.setSuperRate(superRate);
                sm.setSuperWater(superWater);

                sm.setIntervalQuantity(m.getIntervalQuantity());

                sm.setAreaId(m.getAreaId());
                sm.setMeterCode(m.getMeterCode());
                overList.add(sm);
            }
        }
        overVo.setOverCnt(overCnt);
        overVo.setOverList(overList);
        overVo.setTotalCnt(monitorMonths.size());
        return overVo;
    }


















    //水表的用水节约率
    public List<MonitorMeter> queryMonthWater() throws ParseException {
        //DecimalFormat 用于将一个数字进行格式化成字符串；#：没有则为空；0:没有则用0代替
        DecimalFormat df = new DecimalFormat("#0.0");
        //指定日期的格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //查询当前时间
        Calendar instance = Calendar.getInstance();
        //
        instance.set(Calendar.MONTH, 0);//控制月
        instance.set(Calendar.DAY_OF_MONTH, 1);//控制天数
        instance.set(Calendar.HOUR_OF_DAY, 0);//控制时
        instance.set(Calendar.MINUTE, 0);//控制分
        instance.set(Calendar.SECOND, 0);//控制秒
        MonitorMonth param = new MonitorMonth();
        //getTimeInMillis()方法用于获取此Calendar的毫秒数。设置param的上传时间
        param.setUploadTime(new Timestamp(instance.getTimeInMillis()));
        //查出月份的总间隔使用量
        List<MonitorMonth> monitorMonths = monitorMonthMapper.countByMonth(param);
        //新建一个对象集合：一个水表本月用水量
        List<MonitorMeter> monitorMeters = new ArrayList<>();
        //统计日标准总值
        Double standard = meterMapper.sumStandard();
        //查询当前时间
        Calendar cc = Calendar.getInstance();
        //遍历月份的总间隔使用量
        for (MonitorMonth m : monitorMonths) {
            //新建一个对象：一个水表本月用水量
            MonitorMeter mm = new MonitorMeter();
            mm.setUploadDateStr(m.getMonth());//设置上传日期
            mm.setIntervalQuantity(m.getIntervalQuantity());//设置水表本月使用量

            // 获取当月总天数
            cc.setTime(sdf.parse(m.getMonth()));
            //当月天数
            int monthDays = cc.getActualMaximum(Calendar.DAY_OF_MONTH);
            // 算月标准值
            Double totalStandard = standard * monthDays;
            //设置当前月标准用水量
            mm.setMonthMeterStandar(Double.valueOf(df.format(totalStandard)));
            //设置日标准量
            mm.setStandard(totalStandard);
            //如果间隔使用量大于月标准量
            if (m.getIntervalQuantity() >= totalStandard) {
                //将节约率设为0
                mm.setEconomy(0);
                //设置成0%
                mm.setStrEconomy(mm.getEconomy() * 100 + "%");
            } else {
                //设置节约率
                mm.setEconomy(Double.valueOf(df.format((totalStandard - m.getIntervalQuantity()) / totalStandard)));
                mm.setStrEconomy(mm.getEconomy() * 100 + "%");
            }
            monitorMeters.add(mm);
        }
        //返回水表的本月用水量数据
        return monitorMeters;
    }

    //查询当日用量
    public TodayWater queryTodayWater() {
        TodayWater todayWater = new TodayWater();
        /**
         * SimpleDateFormat 是一个格式化Date 以及 解析日期字符串 的工具。
         *
         * 它的最常用途是，能够按照指定的格式来对Date进行格式化，然后我们使用可以格式化Date后得到的字符串。
         * 更严格的说，SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类。
         *
         * 它允许进行格式化（日期 -> 文本）、解析（文本 -> 日期）和规范化。
         */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 查询当前时间
        Calendar instance = Calendar.getInstance();
        Date current = instance.getTime();
        String day = sdf.format(current);
        // 设置总的水表数量
        List<Meter> meters = meterMapper.queryAllMeterWater();
        //总个数等于查询出的总仪器数
        todayWater.setTotalCnt(meters.size());
        //求所有仪器的日标准量的和
        double totalStandard = meters.stream().mapToDouble(Meter::getStandard).sum();
        //实际用水量
        double totalUsage = 0d;
        int overUsage = 0;
        //查询当前时间（日）监控的数据
        List<MonitorDay> monitorDays = monitorDayMapper.queryByDay(day);
        if (null != monitorDays && monitorDays.size() > 0) {
            for (MonitorDay md : monitorDays) {
                //求间隔使用量的总和
                totalUsage += md.getIntervalQuantity();
                //如果取得的日间隔使用量大于标准量
                if (md.getIntervalQuantity() > md.getStandard()) {
                    overUsage++;//统计标签：统计超出标准量的总数
                }
            }
        }

        // 设置当月用水总量
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        MonitorMonth monitorMonth = new MonitorMonth();
        monitorMonth.setMonth(sdf2.format(current));
        //查询当月的监控数据（meter_code：读表器编号
        //    interval_quantity：间隔使用量
        //    month：日期（按月）
        //    device_name：设备名称
        //    another_name：客户编号
        //    area_id:设备所属区域id
        //    area_name:区名称
        //    standard:日标准量）
        List<MonitorMonth> monthList = monitorMonthMapper.query(monitorMonth);
        //获取当月的最后一天
        int actualMaximum = instance.getActualMaximum(Calendar.DAY_OF_MONTH);

        for (MonitorMonth m : monthList) {
            //设置当月的月标准量
            m.setStandard(m.getStandard() * actualMaximum);
        }
        //当月用水总量--按设备位置统计
        todayWater.setMonitorMeters(monthList);
        //获取所有的区名称
        List<Area> allAreas = areaMapper.getAllAreas();
        //DecimalFormat 用于将一个数字进行格式化成字符串；#：没有则为空；0:没有则用0代替
        DecimalFormat df = new DecimalFormat("#0.0");
        // 设置当前标准用水量;Double.valueOf：字符串转换为浮点数
        todayWater.setStandardWater(Double.valueOf(df.format(totalStandard)));
        // 设置当前实际用水量
        todayWater.setCurrentWater(Double.valueOf(df.format(totalUsage)));
        // 统计超标数量
        todayWater.setOverCnt(overUsage);
        // 设置区域
        todayWater.setAreas(allAreas);
        // 设置实际
        todayWater.setDataStr("今日");
        //获取当天是这个月的第几天
        int currentDaySeq = instance.get(Calendar.DAY_OF_MONTH);
        //小于十天按十天算
        if (currentDaySeq <= 10) {
            currentDaySeq = 10;
        }
        //当前天替换一天
        instance.set(Calendar.DAY_OF_MONTH, 1);
        //查询当前时间：年-月-日
        String monthStart = sdf.format(instance.getTime());
        //用当月最后一天日期的值取代当前日
        instance.set(Calendar.DAY_OF_MONTH, instance.getActualMaximum(Calendar.DAY_OF_MONTH));
        //查询当前时间：年-月-日
        String monthEnd = sdf.format(instance.getTime());

        List<MonitorDay> list = new ArrayList<>(); // monitorDayMapper.countByDay(monthStart, monthEnd);
        //求在线仪器的日标准量的和
        double meterStandards = meterMapper.queryAllMeter().stream().mapToDouble(Meter::getStandard).sum();
        //循环遍历当前日期在当月的天数
        for (int i = 1; i <= currentDaySeq; i++) {
            //用i的值取代当前日
            instance.set(Calendar.DAY_OF_MONTH, i);
            //当前时间
            String param = sdf.format(instance.getTime());
            //查询当前时间的间隔使用量
            MonitorDay monitorDay = monitorDayMapper.countByOneDay(param);
            //如果monitorDay为空
            if (Objects.isNull(monitorDay)) {
                monitorDay = new MonitorDay();
                monitorDay.setIntervalQuantity(0D);
                monitorDay.setDay(param);
            }
            //不为空时，将在线仪器的日标准量的和设置到monitorDay中
            monitorDay.setStandard(meterStandards);
            //添加到集合中
            list.add(monitorDay);
        }
        //求list中MonitorDay的间隔使用量的总和
        double monthTotal = list.stream().mapToDouble(MonitorDay::getIntervalQuantity).sum();
        //设置实时流量-立方米/h
        todayWater.setAvg(monthTotal);
        //设置当月用水总量--按天统计
        todayWater.setMonitorCurrentMonths(list);
        return todayWater;
    }

    //根据类型查询当日用水量
    public TodayWater queryTodayByDevice(int deviceTypeCode) {
        //设置小数位数
        DecimalFormat df = new DecimalFormat("#0.0");
        DecimalFormat df1 = new DecimalFormat("#0.000");
        TodayWater todayWater = new TodayWater();
        //设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        // 查询当前时间
        Calendar instance = Calendar.getInstance();
        Date current = instance.getTime();
        String day = sdf.format(current);


        //转换成字符串格式的日期
        String currentMonth = sdf1.format(new Date());
        double monthTotal1=0d;
        //查询当月的用量信息
        List<MonitorMonth> monitorMonths = monitorMonthMapper.queryByMonth1(currentMonth,deviceTypeCode);
        for (MonitorMonth monitorMonth : monitorMonths) {
            monthTotal1+=monitorMonth.getIntervalQuantity();
        }

        // 设置总的仪器数量及其详细信息
        List<Meter> meters = meterMapper.queryAllMeterByDevice(deviceTypeCode);
        //总个数等于查询出的总仪器数
        todayWater.setTotalCnt(meters.size());

        //求所有仪器的日标准量的和
        double totalStandard = meters.stream().mapToDouble(Meter::getStandard).sum();

        //求当前所有仪器的日标准量的和
        //时分
        int hour = instance.get(Calendar.HOUR_OF_DAY);
        int minute = instance.get(Calendar.MINUTE);
        double totalStandardNow=(totalStandard/1440)*(hour*60+minute);

        //实际用水量
        double totalUsage = 0d;
        int overUsage = 0;
        boolean lw=true;
        //查询当前时间（日）监控的数据
        List<MonitorDay> monitorDays = monitorDayMapper.queryByDayByDevice(day,deviceTypeCode);
        if (null != monitorDays && monitorDays.size() > 0) {
            for (MonitorDay md : monitorDays) {
                //求间隔使用量的总和
                totalUsage += Double.valueOf(df.format(md.getIntervalQuantity()));
                //如果取得的日间隔使用量大于标准量
                if (md.getIntervalQuantity() > (md.getStandard()/1440)*(hour*60+minute)) {
                    overUsage++;//统计标签：统计超出标准量的总数
                }
            }
        }
        if (overUsage>todayWater.getTotalCnt()){
            overUsage=(int)todayWater.getTotalCnt();
        }
        //当日节约率
        if (totalStandardNow==totalUsage){
            todayWater.setExcessive(0);
            todayWater.setStandardExcessive(todayWater.getExcessive() * 100 + "%");
        }else{
            //当日节约率
            todayWater.setExcessive(Double.valueOf((Double.valueOf(df.format(totalStandardNow)) - Double.valueOf(df.format(totalUsage))) / Double.valueOf(df.format(totalStandardNow))));
            todayWater.setStandardExcessive(df.format(todayWater.getExcessive() * 100 ) + "%");
        }

        // 设置当月用水总量
        MonitorMonth monitorMonth = new MonitorMonth();
        monitorMonth.setMonth(sdf1.format(current));
        String month = monitorMonth.getMonth();
        List<MonitorMonth> monthList = monitorMonthMapper.queryByDevice(month,deviceTypeCode);
        //获取当月的最后一天
        int actualMaximum = instance.getActualMaximum(Calendar.DAY_OF_MONTH);
        //当前天数
        int currentDaySeq = instance.get(Calendar.DAY_OF_MONTH);
        int currentDaySeq1 = instance.get(Calendar.DAY_OF_MONTH);

        for (MonitorMonth m : monthList) {
            //设置当月的月标准量
            m.setMonthTotal(m.getStandard()*currentDaySeq);
            m.setStandard(m.getStandard() * actualMaximum);
        }
        //当月用水总量--按设备位置统计
        todayWater.setMonitorMeters(monthList);

        //获取所有的区名称
        //List<Area> allAreas = areaMapper.getAllAreas();
        //根据仪器类型查出所有仪器所属区域
        List<Area> allAreasByDevice=areaMapper.getAllAreasByDevice(deviceTypeCode);

        // 设置当前标准用水量;Double.valueOf：字符串转换为浮点数
        todayWater.setStandardWater(Double.valueOf(df.format(totalStandardNow)));
        // 设置当前实际用水量
        todayWater.setCurrentWater(Double.valueOf(df.format(totalUsage)));
        // 统计超标数量
        todayWater.setOverCnt(overUsage);
        // 设置区域
        todayWater.setAreas(allAreasByDevice);
        // 设置实际
        todayWater.setDataStr("今日");
        //获取当天是这个月的第几天


        //小于十天按十天算
        if (currentDaySeq <= 10) {
            currentDaySeq = 10;
        }



        //当前天替换一天
        instance.set(Calendar.DAY_OF_MONTH, 1);

        //用当月最后一天日期的值取代当前日
        instance.set(Calendar.DAY_OF_MONTH, instance.getActualMaximum(Calendar.DAY_OF_MONTH));

        List<MonitorDay> list = new ArrayList<>(); // monitorDayMapper.countByDay(monthStart, monthEnd);
        //求在线仪器的日标准量的和
        double meterStandards = meterMapper.queryAllMeterByDeviceWithStatus(deviceTypeCode).stream().mapToDouble(Meter::getStandard).sum();
        //循环遍历当前日期在当月的天数
        MonitorDay monitorDay=null;
        for (int i = 1; i <= currentDaySeq; i++) {
            //用i的值取代当前日
            instance.set(Calendar.DAY_OF_MONTH, i);
            //当前时间
            String param = sdf.format(instance.getTime());
            //查询当前时间的间隔使用量
             monitorDay = monitorDayMapper.countByOneDayByDevice(param,deviceTypeCode);
            //如果monitorDay为空
            if (Objects.isNull(monitorDay)) {
                monitorDay = new MonitorDay();
                monitorDay.setIntervalQuantity(0D);
                monitorDay.setDay(param);
            }
            //不为空时，将在线仪器的日标准量的和设置到monitorDay中
            monitorDay.setStandard(meterStandards);
            //添加到集合中
            list.add(monitorDay);
        }
        //求list中MonitorDay的间隔使用量的总和
        //double monthTotal = list.stream().mapToDouble(MonitorDay::getIntervalQuantity).sum();
        //设置实时流量-立方米/h
        todayWater.setAvg(monthTotal1);
        //设置当月用水总量--按天统计
        todayWater.setMonitorCurrentMonths(list);

        //以当前时间为标准的月标准量
        todayWater.setMonthStandardWater(Double.valueOf(totalStandard*currentDaySeq1));
        //当月标准量
        todayWater.setSss( Double.valueOf(totalStandard*actualMaximum));
        //当月节约率
        if (todayWater.getMonthStandardWater()==monthTotal1){
            todayWater.setExcessiveMonth(0);
            todayWater.setmExcessiveWater(todayWater.getExcessiveMonth() * 100 + "%");
        }else{
            //当日节约率
            todayWater.setExcessiveMonth(Double.valueOf((todayWater.getMonthStandardWater() - monthTotal1) / todayWater.getMonthStandardWater()));
            todayWater.setmExcessiveWater(df.format(todayWater.getExcessiveMonth() * 100 ) + "%");
        }

        return todayWater;
    }

    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        int currentDaySeq = instance.get(Calendar.DAY_OF_MONTH);
        System.out.println(currentDaySeq);
        instance.set(Calendar.DAY_OF_MONTH, 1);
        currentDaySeq = instance.get(Calendar.DAY_OF_MONTH);
        System.out.println(currentDaySeq);
    }



}
